#include "TreasuryFuturesChart.h"
#include <core/StyleMgr.h>
#include <core/ImageMgr.h>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "../data/TreasuryFuturesData.h"
#include "TreasuryFuturesChartControl.h"
#include "../StringResource.h"

const int kMarkHeight = 24;
const int kMarkMargin = 4;

TreasuryFuturesChart::TreasuryFuturesChart(QWidget* p, CFixedNatFutureData* data)
	: QWidget(p)
{
	QVBoxLayout* mainLayout = new QVBoxLayout(this);
	mainLayout->setContentsMargins(0, 0, 0, kMarkHeight+3*kMarkMargin);
	QHBoxLayout* content = new QHBoxLayout();
	content->setSpacing(2);
	CFixedNatFutureChartControl* yesterday = new CFixedNatFutureChartControl(this, data, true);
	content->addWidget(yesterday);
	CFixedNatFutureChartControl* today = new CFixedNatFutureChartControl(this, data, false);
	content->addWidget(today);
	mainLayout->addLayout(content);
	setLayout(mainLayout);
}

void TreasuryFuturesChart::onRefresh()
{
	QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(this->layout());
	if (!layout) {
		return;
	}

	QLayoutItem* item = layout->itemAt(0);
	if (!item) {
		return;
	}

	QHBoxLayout* content = dynamic_cast<QHBoxLayout*>(item->layout());
	if (!content) {
		return;
	}

	for (int i = 0; i < content->count(); i++) {
		item = content->itemAt(i);
		if (!item) {
			continue;
		}
		CFixedNatFutureChartControl* chart = dynamic_cast<CFixedNatFutureChartControl*>(item->widget());
		if (chart) {
			chart->onRefresh();
		}
	}
}

void TreasuryFuturesChart::paintEvent(QPaintEvent* event)
{
	QStylePainter painter(this);
	drawMark(&painter);

	//中间虚线，区别昨日和今日
	QPen pen(QBrush(QColor(143, 149, 152)), 1, Qt::DotLine);
	painter.setPen(pen);
	QRect rcLine = rect();
	painter.drawLine(QPoint(rcLine.center().x(), rcLine.top()), QPoint(rcLine.center().x(), rcLine.bottom()-(kMarkHeight+2*kMarkMargin)));
}

void TreasuryFuturesChart::drawMark(QStylePainter* painter)
{
	QColor crLine1(250, 194, 70), crLine2(76, 163, 251), crLine3(120, 70, 250), crText(StyleMgr::instance().color("TextNormal"));
	QRect rcMain = rect();
	int nTop = rcMain.bottom() - (kMarkHeight + kMarkMargin);
	int nHeight = kMarkHeight;
	int totalWidth = rcMain.width();
	int markWidth = (totalWidth - (9 + 4) * 2) / 3;

	//价格
	int X = rcMain.left() + 9;
	int Y = nTop + 12;
	QRect rect(QPoint(X , nTop), QPoint(X + markWidth, nTop + nHeight));
	painter->fillRect(rect, StyleMgr::instance().color("StaticTextBK"));
	X += 8;
	painter->setPen(QPen(QBrush(crLine1), 2));
	painter->drawLine(QPoint(X, Y), QPoint(X + 8, Y));
	X += 8 + 4;
	QFont f("Microsoft YaHei");
	f.setPixelSize(12);
	painter->setFont(f);
	painter->setPen(QPen(QBrush(crText), 1));
	painter->drawText(QRect(QPoint(X, nTop), QPoint(rect.right(), nTop+nHeight)), NewStringTable::strFixedNatChart(2), QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

	//成交量
	X = rcMain.left() + 9 + markWidth + 4;
	rect.setLeft(X);
	rect.setRight(X + markWidth);
	painter->fillRect(rect, StyleMgr::instance().color("StaticTextBK"));
	X += 8;
	painter->setPen(QPen(QBrush(crLine2), 2));
	painter->drawLine(QPoint(X, Y), QPoint(X + 8, Y));
	X += 8 + 4;
	painter->setFont(f);
	painter->setPen(QPen(QBrush(crText), 1));
	painter->drawText(QRect(QPoint(X, nTop), QPoint(rect.right(), nTop + nHeight)), NewStringTable::strFixedNatChart(3), QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

	//持仓量
	X = rcMain.left() + 9 + markWidth + 4 + markWidth + 4;
	rect.setLeft(X);
	rect.setRight(X + markWidth);
	painter->fillRect(rect, StyleMgr::instance().color("StaticTextBK"));
	X += 8;
	painter->setPen(QPen(QBrush(crLine3), 2));
	painter->drawLine(QPoint(X, Y), QPoint(X + 8, Y));
	X += 8 + 4;
	painter->setFont(f);
	painter->setPen(QPen(QBrush(crText), 1));
	painter->drawText(QRect(QPoint(X, nTop), QPoint(rect.right(), nTop + nHeight)), NewStringTable::strFixedNatChart(4), QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
}
